package controllers

import (
	"gin-worked/app/system/service"
	"gin-worked/app/system/validate"
	"gin-worked/work"
	"github.com/gin-gonic/gin"
)

type SystemRoleController struct {
}

func (sy SystemRoleController) List(c *gin.Context) {
	var list validate.SystemRoleList

	if err := c.ShouldBind(&list); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.SystemRoleList(param["RoleId"], param["Limit"], param["Offset"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Add 管理员关联角色
func (sy SystemRoleController) Add(c *gin.Context) {
	var add validate.SystemRoleAdd

	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}

	param := c.MustGet("params").(map[string]string)
	res := service.SystemRoleAdd(param["SystemId"], param["RoleId"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Edit 管理员修改关联的角色
func (sy SystemRoleController) Edit(c *gin.Context) {
	var edit validate.SystemRoleEdit

	if err := c.ShouldBind(&edit); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}

	param := c.MustGet("params").(map[string]string)
	res := service.SystemRoleEdit(param["Id"], param["SystemId"], param["RoleId"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Del 删除管理员关联角色
func (sy SystemRoleController) Del(c *gin.Context) {
	var del validate.SystemRoleInfo

	if err := c.ShouldBind(&del); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.SystemRoleDel(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}
